package dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import entity.Group;


public class GroupDAO {
	
	private SqlSessionFactory sqlSessionFactory = null;

	public Integer AddGroup(Group group){
		SqlSession session = sqlSessionFactory.openSession();
		try{
			int r = (Integer)session.insert("entity.Group.insertGroup", group);
			session.commit();
			return r;
		}catch(Exception e){
			e.printStackTrace();
			session.rollback();
			return -1;
		}finally{
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Group> getGroupByuserId(int userid){
		SqlSession session = sqlSessionFactory.openSession();
		try{
			return session.selectList("entity.Group.selectUserGroups", userid);

			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			if(session != null)
				session.close();
		}
	}
	
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

}
